package cn.com.easttech.gofishing;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import cn.com.easttech.gofishing.model.ProductInfo;
import cn.com.easttech.gofishing.util.ImageLoader;

public class ProductInfoActivity extends BaseActivity {
    private static final String PRODUCT_INFO = "productInfo";
    private static final String NEW_PRODUCT_LIST = "newProductList";

    private ProductInfo productInfo;
    private List<ProductInfo> newProductList;

    private RecyclerView mRecyclerView;
    private HomeAdapter mAdapter;

    public static void startAction(Context context, ProductInfo productInfo, List<ProductInfo> newProductList) {
        Intent intent = new Intent(context, ProductInfoActivity.class);
        intent.putExtra(PRODUCT_INFO, productInfo);
        intent.putExtra(NEW_PRODUCT_LIST, (Serializable) newProductList);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_info);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProductInfoActivity.this.finish();
            }
        });

        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        productInfo = (ProductInfo) getIntent().getSerializableExtra(PRODUCT_INFO);
        newProductList = (ArrayList<ProductInfo>) getIntent().getSerializableExtra(NEW_PRODUCT_LIST);

        ImageView productImage = (ImageView) findViewById(R.id.product_image);
        TextView productDesc = (TextView) findViewById(R.id.product_desc);
        ImageView productVideo = (ImageView) findViewById(R.id.product_video);

        ImageLoader.showImage(productInfo.getPictureDesc(), productImage);

        productDesc.setText(Html.fromHtml(productInfo.getTextDesc()));

//        DisplayMetrics metric = new DisplayMetrics();
//        getWindowManager().getDefaultDisplay().getMetrics(metric);
//        int width = metric.widthPixels;

        mRecyclerView = (RecyclerView) findViewById(R.id.new_product_list_recycler);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//        mRecyclerView.setLayoutParams(new LinearLayout.LayoutParams(width / 4, ViewGroup.LayoutParams.MATCH_PARENT));
        mRecyclerView.setAdapter(mAdapter = new HomeAdapter());
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
//        mRecyclerView.addItemDecoration(new DividerGridItemDecoration(this));
//        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        mAdapter.setOnItemClickLitener(new OnItemClickLitener() {

            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(ProductInfoActivity.this, position + " click", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onItemLongClick(View view, int position) {
//                Toast.makeText(MainActivity.this, position + " long click", Toast.LENGTH_SHORT).show();
            }
        });

    }

    class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
        private OnItemClickLitener mOnItemClickLitener;

        public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
            this.mOnItemClickLitener = mOnItemClickLitener;
        }

        private Drawable loadImageFromWebOperations(String url) {
            try {
                InputStream is = (InputStream) new URL(url).getContent();
                Drawable d = Drawable.createFromStream(is, "src name");
                return d;
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
                return null;
            }
        }

        public Bitmap getBitmapFromURL(String src) {
            try {
                Log.d("src", src);
                URL url = new URL(src);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                Bitmap myBitmap = BitmapFactory.decodeStream(input);
                Log.e("Bitmap", "returned");
                return myBitmap;
            } catch (IOException e) {
                Log.e("getBitmapFromURL", e.getMessage());
                return null;
            }
        }

        @Override
        public HomeAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            HomeAdapter.MyViewHolder holder = new HomeAdapter.MyViewHolder(LayoutInflater.from(
                    ProductInfoActivity.this).inflate(R.layout.product_list_main, parent,
                    false));
            return holder;
        }

        @Override
        public void onBindViewHolder(final HomeAdapter.MyViewHolder holder, int position) {
            final ProductInfo productInfo = newProductList.get(position);

            Log.d("onBindViewHolder", productInfo.getPicture());
//            ImageLoader.showImage(productInfo.getPicture(), holder.imageView);

//            holder.imageView.setImageDrawable(loadImageFromWebOperations(productInfo.getPicture()));

//            new Thread() {
//                public void run() {
//                    Bitmap bimage = getBitmapFromURL(productInfo.getPicture());
//                    holder.imageView.setImageBitmap(bimage);
//                }
//            }.start();

            holder.textView.setText(productInfo.getName());

            // 如果设置了回调，则设置点击事件
            if (mOnItemClickLitener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickLitener.onItemClick(holder.itemView, pos);
                    }
                });

                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                        return false;
                    }
                });
            }
        }

        @Override
        public int getItemCount() {
            return newProductList.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {

            ImageView imageView;
            TextView textView;

            public MyViewHolder(View view) {
                super(view);
                imageView = view.findViewById(R.id.product_image);
                textView = view.findViewById(R.id.product_name);
            }
        }
    }

}
